package com.caregiver.watch.business.data.parser;

import com.caregiver.watch.business.data.protobuf.DeviceConf;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class DeviceConfDataParser implements IDataParser {

    @Override
    public Integer getOpt() {
        return null;
    }

    @Override
    public void dataParse(Long userId, String deviceId, byte[] payload) {
        DeviceConf.DeviceConfNotification deviceConfNotify = null;
        try {
            deviceConfNotify = DeviceConf.DeviceConfNotification.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            log.error("parse device conf error", ex);
            return;
        }
        boolean twelve_hours = deviceConfNotify.getHourFormat();
        log.info("time format : {}", twelve_hours ? "12 hours" : "24 hours");
        DeviceConf.DeviceLcdGs lcdGsTime = deviceConfNotify.getLcdGsTime();
        if (Objects.nonNull(lcdGsTime)) {
            boolean lcdGsEnable = deviceConfNotify.getLcdGsSwitch();
            int startHour = lcdGsTime.getLcdGsStartHour();
            int endHour = lcdGsTime.getLcdGsEndHour();
            log.info("lcd gesture: {}, effect hour {} - {}", lcdGsEnable ? "enable" : "disable", startHour, endHour);
        }
    }
}
